package business;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import orm.AORef;
import orm.CollectivityMonthDomain;
import orm.CollectivityMonthDomainAim;
import orm.IHomeworkDomain;
import orm.IHomeworkDomainInfo;
import orm.IndividualHomework;
import orm.IndividualTeachingPlan;
import orm.TeachingPlanBaseInfo;

import dao.AORefDAO;
import dao.IHomeworkDomainDAO;
import dao.IHomeworkDomainInfoDAO;
import dao.IndividualHomeworkDAO;
import dao.IndividualTeachingPlanDAO;
import dao.StudentDAO;
import dao.TPIDMemoryDAO;
import dao.TeacherDAO;
import dao.TeachingPlanBaseInfoDAO;
import util.Utility;
import util.DBHelper;
import util.WebConstants;

public class IndividualTeachingPlanServlet extends BaseServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public IndividualTeachingPlanServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String strFunc = request.getParameter("func");
		
		if (strFunc == null) {
			return;
		} else if (strFunc.equals("load")) {
			if (request.getAttribute("tpid") == null) {
				StudentDAO studentDAO = new StudentDAO();
				TeacherDAO teacherDAO = new TeacherDAO();

				com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
				String teacherName = teacher.getTname();
				Integer teacherID = teacher.getTeacherId();
				
				request.setAttribute("teacherName", teacherName);
				
				Hashtable<Integer, String> ht = null;
				try {
					ht = studentDAO.getStudentsByIndiTeacherID(teacherID);
				} catch (SQLException e) {
					e.printStackTrace();
				}
				request.setAttribute("Students", ht);
				request.getRequestDispatcher("../" + Utility.JSP_IndividualTeachingPlan).forward(request, response);
				//response.sendRedirect("../" + Utility.JSP_IndividualTeachingPlan);
			} else {
				String strTPID = request.getAttribute("tpid").toString();
			}
		} else if (strFunc.equals("save")) {
			TeachingPlanBaseInfo tpbi = new TeachingPlanBaseInfo();
			IndividualTeachingPlan itp = new IndividualTeachingPlan();
			ArrayList<AORef> al = new ArrayList<AORef>();
			IndividualHomework ihw = new IndividualHomework();
			ArrayList<IHomeworkDomain> alIHWD = new ArrayList<IHomeworkDomain>();
			ArrayList<IHomeworkDomainInfo> alIHWDI = new ArrayList<IHomeworkDomainInfo>();
			
			pickupFormData(tpbi, itp, al, ihw, request);
			getIHomeworkDomain(alIHWD, request);
			getIHomeworkDomainInfo(alIHWDI, request);
			insertData(tpbi, itp, al, ihw, alIHWD, alIHWDI);
			
			com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
			
			Integer teacherID = teacher.getTeacherId();

			TeachingPlanBaseInfoDAO tpbiDAO = new TeachingPlanBaseInfoDAO();
			try {
				request.setAttribute("TPList", tpbiDAO.getTPByUserName(teacherID));
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			myTeachingPlan(request, response);
//			request.getRequestDispatcher("../" + Utility.JSP_MyTeachingPlan).forward(request, response);
			response.sendRedirect("IndividualTeachingPlanFunction?func=toFavor");

		} else if (strFunc.equals("edit")) {
			String strTPID = request.getParameter("txtTPID");
			
			TeachingPlanBaseInfo tpbi = new TeachingPlanBaseInfo();
			IndividualTeachingPlan itp = new IndividualTeachingPlan();
			ArrayList<AORef> al = new ArrayList<AORef>();
			IndividualHomework ihw = new IndividualHomework();
			ArrayList<IHomeworkDomain> alIHWD = new ArrayList<IHomeworkDomain>();
			ArrayList<IHomeworkDomainInfo> alIHWDI = new ArrayList<IHomeworkDomainInfo>();
			
			
			pickupFormData(tpbi, itp, al, ihw, request);
			getIHomeworkDomain(alIHWD, request);
			getIHomeworkDomainInfo(alIHWDI, request);
			updateData(tpbi, itp, al, ihw, alIHWD, alIHWDI, strTPID);
			

			//request.getRequestDispatcher("../" + Utility.JSP_MyTeachingPlan).forward(request, response);
			response.sendRedirect("IndividualTeachingPlanFunction?func=toFavor");
		} else if(strFunc.equals("toFavor")){
			
			myTeachingPlan(request, response);
			request.getRequestDispatcher("../" + Utility.JSP_MyTeachingPlan).forward(request, response);
		} else if (strFunc.equals("favor")) {
			TeachingPlanBaseInfo tpbi = new TeachingPlanBaseInfo();
			IndividualTeachingPlan itp = new IndividualTeachingPlan();
			IndividualHomework ihw = new IndividualHomework();
			ArrayList<AORef> al = new ArrayList<AORef>();
			ArrayList<IHomeworkDomain> alIHWD = new ArrayList<IHomeworkDomain>();
			ArrayList<IHomeworkDomainInfo> alIHWDI = new ArrayList<IHomeworkDomainInfo>();

			pickupFormData(tpbi, itp, al, ihw, request);
			getIHomeworkDomain(alIHWD, request);
			getIHomeworkDomainInfo(alIHWDI, request);
			favorData(tpbi, itp, al, ihw, alIHWD, alIHWDI, request.getParameter("txtTPID"));
			
			com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
			
			Integer teacherID = teacher.getTeacherId();
			
			TeachingPlanBaseInfoDAO tpbiDAO = new TeachingPlanBaseInfoDAO();
			try {
				request.setAttribute("TPList", tpbiDAO.getTPByUserName(teacherID));
			} catch (SQLException e) {
				e.printStackTrace();
			}
			myTeachingPlan(request, response);
			request.getRequestDispatcher("../" + Utility.JSP_MyTeachingPlan).forward(request, response);
			
			
		} else if (strFunc.equals("printITP")) {
			
			String strTPID = request.getParameter("txtTPID");
			TeacherDAO teacherDAO = new TeacherDAO();
			
			com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
			
			Integer teacherID = teacher.getTeacherId();
			String teacherName = teacher.getTname();
			request.setAttribute("teacherName", teacherName);
			
			TeachingPlanBaseInfo tpbi = new TeachingPlanBaseInfo();
			IndividualTeachingPlan itp = new IndividualTeachingPlan();
			ArrayList<AORef> al = new ArrayList<AORef>();
			
			pickupFormDataPrint(tpbi, itp, al, request);
			
			request.setAttribute(WebConstants.TeachingPlanBaseInfo, tpbi);
			request.setAttribute(WebConstants.IndividualTeachingPlan , itp);
			request.setAttribute(WebConstants.AORef , al);
			
			request.getRequestDispatcher("../" + Utility.JSP_PrintITP).forward(request, response);
		} else if (strFunc.equals("printITPHomework")) {
			String strTPID = request.getParameter("txtTPID");
			TeacherDAO teacherDAO = new TeacherDAO();
			
			com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
			
			Integer teacherID = teacher.getTeacherId();
			String teacherName = teacher.getTname();
			request.setAttribute("teacherName", teacherName);
			for(int DomainID = 5; DomainID < 11; DomainID++){
				for(int AimID = 5; AimID > 0; AimID--){ 
					int index = Math.abs(AimID-6);
					
					int completeEntering = Integer.parseInt(request.getParameter("CompleteEntering"+DomainID+index));
					ArrayList<String> alCompleteEntering = Utility.CONSTANT_COMPLETEENTERING;
					String CompleteEntering = null;
			       	for (int i = 0; i<alCompleteEntering.size(); i++) {
			       		if (i == completeEntering-1) {
			       			CompleteEntering = alCompleteEntering.get(i);
			       		}
			   		}
			       	request.setAttribute("completeEnteringPrint"+DomainID+index, CompleteEntering);
				}
			}
			TeachingPlanBaseInfo tpbi = new TeachingPlanBaseInfo();
			IndividualTeachingPlan itp = new IndividualTeachingPlan();
			ArrayList<AORef> al = new ArrayList<AORef>();
			IndividualHomework ihw = new IndividualHomework();
			ArrayList<IHomeworkDomain> alIHWD = new ArrayList<IHomeworkDomain>();
			ArrayList<IHomeworkDomainInfo> alIHWDI = new ArrayList<IHomeworkDomainInfo>();
			
//			pickupFormData(tpbi, itp, al, ihw, request);
			pickupFormDataPrintIHW(ihw, request);
			getIHomeworkDomain(alIHWD, request);
			getIHomeworkDomainInfo(alIHWDI, request);
			
			request.setAttribute(WebConstants.IndividualHomework, ihw);
			request.setAttribute(WebConstants.IHomeworkDomain , alIHWD);
			request.setAttribute(WebConstants.IHomeworkDomainInfo , alIHWDI);
			
			request.getRequestDispatcher("../" + Utility.JSP_PrintITPHomework).forward(request, response);
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	public void pickupFormData(TeachingPlanBaseInfo tpbi, IndividualTeachingPlan itp,
			ArrayList<AORef> aoRefArr, IndividualHomework ihw, HttpServletRequest request) {
		com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
		
		Integer teacherID = teacher.getTeacherId();
		String teacherName = teacher.getTname();
		
		tpbi.setTpMaterial(Utility.TEACHINGPLAN_MATERIAL_TEXT);
		tpbi.setTpProperty(Integer.parseInt(request.getParameter("rdoTPProp")));
		tpbi.setTpType(Utility.TEACHINGPLAN_TYPE_INDIVIDUAL);
		tpbi.setConstitutor(teacherName);
		String strWroteDate = request.getParameter("txtWroteDateYear");
		tpbi.setWroteDate(strWroteDate);
		String strExecutionDate = request.getParameter("txtExecutionDateYear");
		tpbi.setExecutionDate(strExecutionDate);
		tpbi.setAppropriateAge(Integer.parseInt(request.getParameter("sltAppropriateAge")));
		TeacherDAO teacherDAO = new TeacherDAO();
		
		tpbi.setTeacherID(teacherID);
		String[] strArrApproStu = request.getParameterValues("cbAppropriateStudents");
		StringBuffer sb = new StringBuffer();
		StringBuffer sbName = new StringBuffer();
		StudentDAO studentDAO = new StudentDAO();

		Hashtable<Integer, String> ht = null;
		try {
//			ht = studentDAO.getStudentsByTeacherID(teacherID);
			ht = studentDAO.getStudentsByIndiTeacherID(teacherID);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(strArrApproStu != null){
			for (int i = 0; i < strArrApproStu.length; i++) {
				sb.append(strArrApproStu[i]);
				if (i != strArrApproStu.length -1) {
					sb.append(",");
				}
				sbName.append(ht.get(strArrApproStu[i]) );
				if (i != strArrApproStu.length -1) {
					sbName.append(",");
				}
			}
		}
		
		
		tpbi.setAppropriateStudents(sb.toString());
		tpbi.setAppropriateStudentNames(sbName.toString());
		
//		String[] strArrApproStu = request.getParameterValues("cbAppropriateStudents");
//		StringBuffer sb = new StringBuffer();
//		if(strArrApproStu != null){
//			for (int i = 0; i < strArrApproStu.length; i++) {
//				sb.append(strArrApproStu[i]);
//				if (i != strArrApproStu.length -1) {
//					sb.append(",");
//				}
//			}
//		}
//		
//		tpbi.setAppropriateStudents(sb.toString());
		
		tpbi.setActivityName(request.getParameter("txtActivityName"));
		tpbi.setMonthThemeName(request.getParameter("MonthThemeName"));
		tpbi.setWeekThemeName(request.getParameter("WeekThemeName"));
		tpbi.setActivityType(Integer.parseInt(request.getParameter("rdoActivityType")));
		tpbi.setShare(Utility.BOOLEAN_FALSE);
		tpbi.setGrade(0);
		tpbi.setEditingTimes(0);
		
		itp.setCurrentHearingAge(request.getParameter("txtCurrentHearingAge"));
		itp.setCurrentLanguageAge(request.getParameter("txtCurrentLanguageAge"));
		itp.setCurrentCognizanceAge(request.getParameter("txtCurrentCognizanceAge"));
		itp.setCurrentAge(request.getParameter("txtCurrentAge"));
		itp.setAggrandizementDomain(Integer.parseInt(request.getParameter("sltAggrandizementDomain")));
		itp.setCorePurpose(Integer.parseInt(request.getParameter("rdoCorePurpose")));
		itp.setActivityPreparation(request.getParameter("taActivityPreparation"));
		itp.setTeachingWay(Integer.parseInt(request.getParameter("rdoTeachingWay")));
		itp.setTeachingTime(request.getParameter("txtTeachingTime"));
		itp.setTeacherAssignment(request.getParameter("taTeacherAssignment"));
		itp.setParentsAssignment(request.getParameter("taParentsAssignment"));
		itp.setActivityStepReporting(request.getParameter("taActivityStepReporting"));
		itp.setTeachingMaterial(request.getParameter("taTeachingMaterial"));
		itp.setTeacherExperience(request.getParameter("taTeachingExperience"));
		itp.setParentsBemusement(request.getParameter("taParentsBemusement"));
		itp.setInstructionSuggestion(request.getParameter("taInstructionSuggestion"));
		
		AORef aoRef = null;
		String[] strArrBehaviorJudgement;
		for (int i = 1; i <= Utility.CONSTANT_ACTIVEOPTION.size(); i++) {
			aoRef = new AORef();
			aoRef.setAoID(i);
			aoRef.setActivityPurpose(request.getParameter("taActivityPurpose" + i));
			aoRef.setExistentFoundation(request.getParameter("taExistentFoundation" + i));
			aoRef.setPracticalBehavior(request.getParameter("taPracticalBehavior" + i));
			strArrBehaviorJudgement = request.getParameterValues("cbBehaviorJudgement" + i);
			if (strArrBehaviorJudgement != null && strArrBehaviorJudgement.length != 0) {
				for (int j = 0; j < strArrBehaviorJudgement.length; j++) {
					switch (Integer.parseInt(strArrBehaviorJudgement[j])) {
						case 1:
							aoRef.setImitation(Utility.BOOLEAN_TRUE);
							break;
						case 2:
							aoRef.setUnderstanding(Utility.BOOLEAN_TRUE);
							break;
						case 3:
							aoRef.setExpression(Utility.BOOLEAN_TRUE);
							break;
						case 4:
							aoRef.setApplication(Utility.BOOLEAN_TRUE);
							break;
					}
				}
			}
			aoRefArr.add(aoRef);
		}
		
		String[] strArrApproStuIHW = request.getParameterValues("cbAppropriateStudentsIHW");
		StringBuffer sbIHW = new StringBuffer();
		StringBuffer sbNameIHW = new StringBuffer();
		

		Hashtable<Integer, String> htIHW = null;
		try {
//			htIHW = studentDAO.getStudentsByTeacherID(teacherID);
			htIHW = studentDAO.getStudentsByIndiTeacherID(teacherID);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(strArrApproStuIHW != null){
			for (int i = 0; i < strArrApproStuIHW.length; i++) {
				sbIHW.append(strArrApproStuIHW[i]);
				if (i != strArrApproStuIHW.length -1) {
					sbIHW.append(",");
				}
				sbNameIHW.append(htIHW.get(strArrApproStuIHW[i]) );
				if (i != strArrApproStuIHW.length -1) {
					sbNameIHW.append(",");
				}
			}
		}
		

		ihw.setAppropriateStudents(sbIHW.toString());
		ihw.setAppropriateStudentNames(sbNameIHW.toString());
		ihw.setHomeworkDate(request.getParameter("HomeworkDate"));
		ihw.setTeacherName(teacherName);
		ihw.setTeacherID(teacherID);
		
	}
	
	public void pickupFormDataPrint(TeachingPlanBaseInfo tpbi, IndividualTeachingPlan itp, ArrayList<AORef> aoRefArr, HttpServletRequest request) {
		tpbi.setTpMaterial(Utility.TEACHINGPLAN_MATERIAL_TEXT);
		com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
		
		Integer teacherID = teacher.getTeacherId();
		String teacherName = teacher.getTname();
		
		String dddd = request.getParameter("rdoTPProp");
		tpbi.setTpProperty(Integer.parseInt(request.getParameter("rdoTPProp")));
		tpbi.setTpType(Utility.TEACHINGPLAN_TYPE_INDIVIDUAL);
		tpbi.setConstitutor(teacherName);
		String strDateTmp = request.getParameter("txtWroteDateYear")  + "-" + request.getParameter("txtWroteDateMonth") +
			"-" + request.getParameter("txtWroteDateDay");
		tpbi.setWroteDate(strDateTmp);
		strDateTmp = request.getParameter("txtExecutionDateYear") + "-" + request.getParameter("txtExecutionDateMonth") + 
			"-" + request.getParameter("txtExecutionDateDay");
		tpbi.setExecutionDate(strDateTmp);
		tpbi.setAppropriateAge(Integer.parseInt(request.getParameter("sltAppropriateAge")));
		TeacherDAO teacherDAO = new TeacherDAO();
		
		tpbi.setTeacherID(teacherID);

		
		String[] strArrApproStu = request.getParameterValues("cbAppropriateStudents");
		StringBuffer sb = new StringBuffer();
		StringBuffer sbName = new StringBuffer();
		StudentDAO studentDAO = new StudentDAO();

		Hashtable<Integer, String> ht = null;
		try {
//			ht = studentDAO.getStudentsByTeacherID(teacherID);
			ht = studentDAO.getStudentsByIndiTeacherID(teacherID);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(strArrApproStu != null){
			for (int i = 0; i < strArrApproStu.length; i++) {
				sb.append(strArrApproStu[i]);
				if (i != strArrApproStu.length -1) {
					sb.append(",");
				}
				if(strArrApproStu[i]!=null && !strArrApproStu[i].equals("")){
					
					sbName.append(ht.get(new Integer(strArrApproStu[i])));
				}
				if (i != strArrApproStu.length -1) {
					sbName.append(",");
				}
			}
		}
		
		
		tpbi.setAppropriateStudents(sb.toString());
		tpbi.setAppropriateStudentNames(sbName.toString());
		
		tpbi.setActivityName(request.getParameter("txtActivityName"));
		tpbi.setMonthThemeName(request.getParameter("MonthThemeName"));
		tpbi.setWeekThemeName(request.getParameter("WeekThemeName"));
		tpbi.setTpID(request.getParameter("txtTPID"));
		ArrayList<String> alActivityType = Utility.CONSTANT_ACTIVITYTYPE;
		String strActivityType = null;
		int intActivityType = Integer.parseInt(request.getParameter("rdoActivityType"));
       	for (int i = 0; i<alActivityType.size(); i++) {
       		if (i == intActivityType-1) {
       			strActivityType = alActivityType.get(i);
       		}
   		}
       	request.setAttribute("strActivityType", strActivityType);
//		tpbi.setActivityType(Integer.parseInt(request.getParameter("rdoActivityType")));
		tpbi.setShare(Utility.BOOLEAN_FALSE);
		tpbi.setGrade(0);
		tpbi.setEditingTimes(0);
		
		itp.setCurrentHearingAge(request.getParameter("txtCurrentHearingAge"));
		itp.setCurrentLanguageAge(request.getParameter("txtCurrentLanguageAge"));
		itp.setCurrentCognizanceAge(request.getParameter("txtCurrentCognizanceAge"));
		itp.setCurrentAge(request.getParameter("txtCurrentAge"));
		int aggrandizementDomain = Integer.parseInt(request.getParameter("sltAggrandizementDomain"));
		ArrayList<String> al = Utility.CONSTANT_ACTIVEOPTION;
		String AggrandizementDomain = null;
       	for (int i = 0; i<al.size(); i++) {
       		if (i == aggrandizementDomain-1) {
       			AggrandizementDomain = al.get(i);
       		}
   		}
       	request.setAttribute("aggrandizementDomainPrint", AggrandizementDomain);
//		itp.setAggrandizementDomain(Integer.parseInt(request.getParameter("sltAggrandizementDomain")));
       	int intCorePurpose = Integer.parseInt(request.getParameter("rdoCorePurpose"));
		ArrayList<String> alCorePurpose = Utility.CONSTANT_ACTIVEOPTION;
		String strCorePurpose = null;
       	for (int i = 0; i<alCorePurpose.size(); i++) {
       		if (i == intCorePurpose-1) {
       			strCorePurpose = alCorePurpose.get(i);
       		}
   		}
       	request.setAttribute("strCorePurpose", strCorePurpose);
//		itp.setCorePurpose(Integer.parseInt(request.getParameter("rdoCorePurpose")));
		itp.setActivityPreparation(request.getParameter("taActivityPreparation"));
//		itp.setTeachingWay(Integer.parseInt(request.getParameter("rdoTeachingWay")));
		int intTeachingWay = Integer.parseInt(request.getParameter("rdoTeachingWay"));
		ArrayList<String> alTeachingWay = Utility.CONSTANT_TEACHINGWAY;
		String strTeachingWay = null;
       	for (int i = 0; i<alTeachingWay.size(); i++) {
       		if (i == intTeachingWay-1) {
       			strTeachingWay = alTeachingWay.get(i);
       		}
   		}
       	request.setAttribute("strTeachingWay", strTeachingWay);
		itp.setTeachingTime(request.getParameter("txtTeachingTime"));
		itp.setTeacherAssignment(request.getParameter("taTeacherAssignment"));
		itp.setParentsAssignment(request.getParameter("taParentsAssignment"));
		itp.setActivityStepReporting(request.getParameter("taActivityStepReporting"));
		itp.setTeachingMaterial(request.getParameter("taTeachingMaterial"));
		itp.setTeacherExperience(request.getParameter("taTeachingExperience"));
		itp.setParentsBemusement(request.getParameter("taParentsBemusement"));
		itp.setInstructionSuggestion(request.getParameter("taInstructionSuggestion"));
		
		AORef aoRef = null;
		String[] strArrBehaviorJudgement;
		for (int i = 1; i <= Utility.CONSTANT_ACTIVEOPTION.size(); i++) {
			aoRef = new AORef();
			aoRef.setAoID(i);
			aoRef.setActivityPurpose(request.getParameter("taActivityPurpose" + i));
			request.setAttribute("ActivityPurpose"+i, request.getParameter("taActivityPurpose" + i));
			aoRef.setExistentFoundation(request.getParameter("taExistentFoundation" + i));
			request.setAttribute("ExistentFoundation"+i, request.getParameter("taExistentFoundation" + i));
			aoRef.setPracticalBehavior(request.getParameter("taPracticalBehavior" + i));
			request.setAttribute("PracticalBehavior"+i, request.getParameter("taPracticalBehavior" + i));
			strArrBehaviorJudgement = request.getParameterValues("cbBehaviorJudgement" + i);
			if (strArrBehaviorJudgement != null && strArrBehaviorJudgement.length != 0) {
				for (int j = 0; j < strArrBehaviorJudgement.length; j++) {
					switch (Integer.parseInt(strArrBehaviorJudgement[j])) {
						case 1:
							aoRef.setImitation(Utility.BOOLEAN_TRUE);
//							request.setAttribute("Imitation"+j, o)
							break;
						case 2:
							aoRef.setUnderstanding(Utility.BOOLEAN_TRUE);
							break;
						case 3:
							aoRef.setExpression(Utility.BOOLEAN_TRUE);
							break;
						case 4:
							aoRef.setApplication(Utility.BOOLEAN_TRUE);
							break;
					}
				}
			}
			aoRefArr.add(aoRef);
		}
	}
	
	
	public void pickupFormDataPrintIHW(IndividualHomework ihw, HttpServletRequest request) {
		com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
		
		Integer teacherID = teacher.getTeacherId();
		String teacherName = teacher.getTname();
		
		StudentDAO studentDAO = new StudentDAO();
		
		String[] strArrApproStuIHW = request.getParameterValues("cbAppropriateStudentsIHW");
		StringBuffer sbIHW = new StringBuffer();
		StringBuffer sbNameIHW = new StringBuffer();
		

		Hashtable<Integer, String> htIHW = null;
		try {
//			htIHW = studentDAO.getStudentsByTeacherID(teacherID);
			htIHW = studentDAO.getStudentsByIndiTeacherID(teacherID);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(strArrApproStuIHW != null){
			for (int i = 0; i < strArrApproStuIHW.length; i++) {
				sbIHW.append(strArrApproStuIHW[i]);
				if (i != strArrApproStuIHW.length -1) {
					sbIHW.append(",");
				}
				if(strArrApproStuIHW[i]!=null && !strArrApproStuIHW[i].equals("")){
					
					sbNameIHW.append(htIHW.get(new Integer(strArrApproStuIHW[i])));
				}
				if (i != strArrApproStuIHW.length -1) {
					sbNameIHW.append(",");
				}
			}
		}
		

		ihw.setAppropriateStudents(sbIHW.toString());
		ihw.setAppropriateStudentNames(sbNameIHW.toString());
		ihw.setHomeworkDate(request.getParameter("HomeworkDate"));
		ihw.setTeacherName(teacherName);
		ihw.setTeacherID(teacherID);
		
	}
	
	public void getIHomeworkDomain(ArrayList<IHomeworkDomain> ihwdArr, HttpServletRequest request){
		for(int domainID = 5; domainID < 11; domainID++){
			IHomeworkDomain ihwd = new IHomeworkDomain();
			ihwd.setDomainID(domainID);
			ihwd.setRemark(request.getParameter("Remark" + domainID));
			ihwdArr.add(ihwd);
		}
	}
	public void getIHomeworkDomainInfo(ArrayList<IHomeworkDomainInfo> ihwdiArr, HttpServletRequest request){
		for(int domainID = 5; domainID < 11; domainID++){
			for(int aimID = 1; aimID < 6; aimID++){
				IHomeworkDomainInfo ihwdi = new IHomeworkDomainInfo();
				ihwdi.setDomainID(domainID);
				ihwdi.setAimID(aimID);
				ihwdi.setTaskAim(request.getParameter("TaskAim"+domainID+aimID));
				ihwdi.setContentMaterial(request.getParameter("ContentMaterial"+domainID+aimID));
				ihwdi.setLearnToHave(request.getParameter("LearnToHave"+domainID+aimID));
				ihwdi.setGuidingMeans(request.getParameter("GuidingMeans"+domainID+aimID));
				ihwdi.setCompleteEntering(Integer.parseInt(request.getParameter("CompleteEntering" +domainID+aimID)));
						
				ihwdiArr.add(ihwdi);
			}
		}	
	}

	public void insertData(TeachingPlanBaseInfo tpbi, IndividualTeachingPlan itp,
			ArrayList<AORef> aoRefArr, IndividualHomework ihw, ArrayList<IHomeworkDomain> ihwdArr,
			ArrayList<IHomeworkDomainInfo> ihwdiArr) {
		Connection conn = DBHelper.getConnection();
		Statement stmt = null;
		
		try {
			stmt = conn.createStatement();
			conn.setAutoCommit(false);
			
			TPIDMemoryDAO tpidMemDAO = new TPIDMemoryDAO();
			int nTpid = tpidMemDAO.increaseTPID(stmt);
			String strTpid = "ABCD11" + nTpid;
			
			TeachingPlanBaseInfoDAO tpbiDAO = new TeachingPlanBaseInfoDAO();
			tpbi.setTpID(strTpid);
			tpbiDAO.insertTeachingPlanBaseInfo(stmt, tpbi);
			
			IndividualTeachingPlanDAO itpDAO = new IndividualTeachingPlanDAO();
			itp.setTpID(strTpid);
			itpDAO.insertITP(stmt, itp);
			
			AORefDAO aoRefDAO = new AORefDAO();
			aoRefDAO.insertAORef(stmt, aoRefArr, strTpid);
			
			IndividualHomeworkDAO ihwDAO = new IndividualHomeworkDAO();
			ihw.setTPID(strTpid);
			ihwDAO.insertIHW(stmt, ihw);
			
			IHomeworkDomainDAO ihwdDAO = new IHomeworkDomainDAO();
			ihwdDAO.insertIHomeworkDomain(stmt, ihwdArr, strTpid);
			
			IHomeworkDomainInfoDAO ihwdiDAO = new IHomeworkDomainInfoDAO();
			ihwdiDAO.insertIHomeworkDomainInfo(stmt, ihwdiArr, strTpid);
			
			conn.commit();
			
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException ee) {
				
			}
		}
	}

	public void updateData(TeachingPlanBaseInfo tpbi, IndividualTeachingPlan itp, ArrayList<AORef> aoRefArr,
			IndividualHomework ihw, ArrayList<IHomeworkDomain> ihwdArr,
			ArrayList<IHomeworkDomainInfo> ihwdiArr, String tpid) {
		Connection conn = DBHelper.getConnection();
		Statement stmt = null;
		try {
			stmt = conn.createStatement();
			conn.setAutoCommit(false);
			
			TeachingPlanBaseInfoDAO tpbiDAO = new TeachingPlanBaseInfoDAO();
			tpbi.setTpID(tpid);
			tpbiDAO.updateTPByTPID(stmt, tpbi);
			
			IndividualTeachingPlanDAO itpDAO = new IndividualTeachingPlanDAO();
			itp.setTpID(tpid);
			itpDAO.updateITP(stmt, itp);
			
			AORefDAO aoRefDAO = new AORefDAO();
			aoRefDAO.updateAORef(stmt, aoRefArr, tpid);
			
			IndividualHomeworkDAO ihwDAO = new IndividualHomeworkDAO();
			ihw.setTPID(tpid);
			ihwDAO.updateIHW(stmt, ihw);
			
			IHomeworkDomainDAO ihwdDAO = new IHomeworkDomainDAO();
			ihwdDAO.updateIHomeworkDomain(stmt, ihwdArr, tpid);
			
			IHomeworkDomainInfoDAO ihwdiDAO = new IHomeworkDomainInfoDAO();
			ihwdiDAO.updateIHomeworkDomainInfo(stmt, ihwdiArr, tpid);
			
			conn.commit();
			
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException ee) {
				
			}
		}
	}

	public void favorData(TeachingPlanBaseInfo tpbi, IndividualTeachingPlan itp, ArrayList<AORef> aoRefArr,
			IndividualHomework ihw, ArrayList<IHomeworkDomain> ihwdArr,
			ArrayList<IHomeworkDomainInfo> ihwdiArr, String tpid) {
		Connection conn = DBHelper.getConnection();
		Statement stmt = null;
		
		try {
			stmt = conn.createStatement();
			conn.setAutoCommit(false);
			
			TPIDMemoryDAO tpidMemDAO = new TPIDMemoryDAO();
			int nTpid = tpidMemDAO.increaseTPID(stmt);
			String strTpid = tpid + Utility.TPID_CONNNECTSTRING + "ABCD11" + nTpid;
			
			TeachingPlanBaseInfoDAO tpbiDAO = new TeachingPlanBaseInfoDAO();
			tpbi.setTpID(strTpid);
			tpbiDAO.insertTeachingPlanBaseInfo(stmt, tpbi);
			
			IndividualTeachingPlanDAO itpDAO = new IndividualTeachingPlanDAO();
			itp.setTpID(strTpid);
			itpDAO.insertITP(stmt, itp);
			
			AORefDAO aoRefDAO = new AORefDAO();
			aoRefDAO.insertAORef(stmt, aoRefArr, strTpid);
			
			IndividualHomeworkDAO ihwDAO = new IndividualHomeworkDAO();
			ihw.setTPID(strTpid);
			ihwDAO.insertIHW(stmt, ihw);
			
			IHomeworkDomainDAO ihwdDAO = new IHomeworkDomainDAO();
			ihwdDAO.insertIHomeworkDomain(stmt, ihwdArr, strTpid);
			
			IHomeworkDomainInfoDAO ihwdiDAO = new IHomeworkDomainInfoDAO();
			ihwdiDAO.insertIHomeworkDomainInfo(stmt, ihwdiArr, strTpid);
			
			conn.commit();
			
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException ee) {
				
			}
		}
	}
	public void myTeachingPlan(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException{
	TeachingPlanBaseInfoDAO tpbiDAO = new TeachingPlanBaseInfoDAO();
	com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
	
	Integer teacherID = teacher.getTeacherId();
	String teacherName = teacher.getTname();
	try {
		
		
		request.setAttribute("TPList", tpbiDAO.getTPByUserName(teacherID));
		ArrayList coll = new ArrayList();
		coll = tpbiDAO.getCollectTPByUserName(teacherID);
		if(coll != null){
		request.setAttribute("collectionTPList", tpbiDAO.getCollectTPByUserName(teacherID));
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}
	
	
	TeachingPlanBaseInfo tpbi = null;

	try {
		request.setAttribute("pageNo", 1);
		int PageSize = Utility.PageSize;
		ArrayList al = tpbiDAO.getTPByUserName(teacherID);
		int totalPage = 0;
		if (al.size() % PageSize == 0) {
			totalPage = al.size()/PageSize;
			request.setAttribute("totalPage",totalPage);
		} else {
			totalPage = (al.size()/PageSize) + 1;
			request.setAttribute("totalPage",totalPage);
		}
		
		al = new ArrayList<TeachingPlanBaseInfo>();
		ListIterator<TeachingPlanBaseInfo> it = tpbiDAO.getTPByUserName(teacherID).listIterator();
		int ii = 0;
		while (it.hasNext()) {
			TeachingPlanBaseInfo t = it.next();
			tpbi = new TeachingPlanBaseInfo();
			tpbi.setActivityName(t.getActivityName());
			tpbi.setEditingTimes(t.getEditingTimes());
			tpbi.setTeacherName(t.getTeacherName());
			tpbi.setTpType(t.getTpType());
			tpbi.setTpID(t.getTpID());
			al.add(tpbi);
			ii++;

			if(ii > (PageSize-1) ) {
				break;
			}
		}
		
		request.setAttribute("TPList", al);
		
		ArrayList coll = new ArrayList();
		coll = tpbiDAO.getCollectTPByUserName(teacherID);
		if(coll != null){
		request.setAttribute("collectionTPList", tpbiDAO.getCollectTPByUserName(teacherID));
		}
		
	} catch (SQLException e) {
		e.printStackTrace();
	}
}
}
